package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.DeviceDetailInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceWranInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.IDeviceWranInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IDeviceWranInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;


@Service
@Transactional
@SuppressWarnings("rawtypes")
public class DeviceWranInfoService implements IDeviceWranInfoService {

    @Autowired
    private IDeviceWranInfoMapper deviceWranInfoMapper;

    @Autowired
    private IPubLogMapper pubLogMapper;

    @Override
    public DeviceWranInfo selectDeviceWranInfoById(String devWranId){
        return deviceWranInfoMapper.selectById(devWranId);
    }
    
    @Override
    public DeviceWranInfo selectDeviceWranInfoByKey(String devCategoryCode, String devTypeCode, String devId, 
    		String wranTypeCode, String isRecover){
    	try {
    		DeviceWranInfo deviceWranInfo = null;
    		List<DeviceWranInfo> deviceWranInfoList = this.selectDeviceWranInfoList(null, devCategoryCode, 
    				devTypeCode, devId, null, null, wranTypeCode, null, null, isRecover, null, null);
	    	if(null != deviceWranInfoList && deviceWranInfoList.size() > 0) {
	    		deviceWranInfo = deviceWranInfoList.get(0);
	    	}
	    	return deviceWranInfo;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
	public List<DeviceWranInfo> selectDeviceWranInfoList(String devWranId, String devCategoryCode, String devTypeCode,
			String devId, String devName, String key, String wranTypeCode, String wranLevelCode, String wranContent,
			String isRecover, String wranStartTime, String wranEndTime) {
    	try {
    		List<DeviceWranInfo> deviceWranInfoList = new ArrayList<DeviceWranInfo>(); 
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("devWranId", devWranId);
	    	paramT.put("devCategoryCode", devCategoryCode);
	    	paramT.put("devTypeCode", devTypeCode);
	    	paramT.put("devId", devId);
	    	paramT.put("devName", devName);
	    	paramT.put("key", key);
	    	paramT.put("wranTypeCode", wranTypeCode);
	    	paramT.put("wranLevelCode", wranLevelCode);
	    	paramT.put("wranContent", wranContent);
	    	paramT.put("wranStartTime", wranStartTime);
	    	paramT.put("wranEndTime", wranEndTime);
	    	if("0".equals(isRecover)) {
	    		deviceWranInfoList = deviceWranInfoMapper.selectDeviceWranRealList(paramT);
	    	}else if("1".equals(isRecover)) {
	    		deviceWranInfoList = deviceWranInfoMapper.selectDeviceWranHisList(paramT);
	    	}else {
	    		deviceWranInfoList = deviceWranInfoMapper.selectDeviceWranInfoList(paramT);
	    	}
	    	return deviceWranInfoList;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
	
    @SuppressWarnings("unchecked")
	@Override
	public Page selectDeviceWranInfoPage(Page page, String devWranId, String devCategoryCode, String devTypeCode,
			String devId, String devName, String key, String wranTypeCode, String wranLevelCode, String wranContent,
			String isRecover, String wranStartTime, String wranEndTime) {
    	try {
	    	Map<String,String> mapBean = new HashMap<String,String>();
	    	mapBean.put("devWranId", devWranId);
	    	mapBean.put("devCategoryCode", devCategoryCode);
	    	mapBean.put("devTypeCode", devTypeCode);
	    	mapBean.put("devId", devId);
	    	mapBean.put("devName", devName);
	    	mapBean.put("key", key);
	    	mapBean.put("wranTypeCode", wranTypeCode);
	    	mapBean.put("wranLevelCode", wranLevelCode);
	    	mapBean.put("wranContent", wranContent);
	    	mapBean.put("wranStartTime", wranStartTime);
	    	mapBean.put("wranEndTime", wranEndTime);
	    	page.setMapBean(mapBean);
	    	if("0".equals(isRecover)) {
	    		page = deviceWranInfoMapper.selectDeviceWranRealPage(page);
	    	}else if("1".equals(isRecover)) {
	    		page = deviceWranInfoMapper.selectDeviceWranHisPage(page);
	    	}else {
	    		page = deviceWranInfoMapper.selectPageInfo(page);
	    	}
	        return page;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String insertDeviceWranInfo(DeviceWranInfo deviceWranInfo, String personName){
    	try {
	    	// 默认操作记录信息
	        String devWranId = "DW" + TimeHelper.getUniqueString();
	        deviceWranInfo.setDevWranId(devWranId);
	        //deviceWranInfo.setIsRecover(false);
	        deviceWranInfo.setWranTime(TimeHelper.dateToLongString());
	        deviceWranInfo.setUpdateTime(TimeHelper.dateToLongString());
	        deviceWranInfoMapper.insert(deviceWranInfo);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                String.format("添加设备报警编号:{%s} 操作成功!", new Object[] { devWranId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WRAN_MANAGEMENT.getMessage());
	        return devWranId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String insertDeviceWranInfoHis(DeviceWranInfo deviceWranInfoHis, String personName){
    	try {
	    	// 默认操作记录信息
	        String devWranHisId = "DH" + TimeHelper.getUniqueString();
	        deviceWranInfoHis.setDevWranId(devWranHisId);
	        if(null == deviceWranInfoHis.getWranTime()) {
	        	deviceWranInfoHis.setWranTime(TimeHelper.dateToLongString());
	        }
	        deviceWranInfoHis.setUpdateTime(TimeHelper.dateToLongString());
	        deviceWranInfoMapper.insertWranHis(deviceWranInfoHis);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                String.format("添加设备报警历史编号:{%s} 操作成功!", new Object[] { devWranHisId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WRAN_MANAGEMENT.getMessage());
	        return devWranHisId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String updateDeviceWranInfo(DeviceWranInfo deviceWranInfo, String personName){
    	try {
	    	String devWranId = deviceWranInfo.getDevWranId();
	        deviceWranInfo.setUpdateTime(TimeHelper.dateToLongString());
	    	deviceWranInfoMapper.updateByIdSelective(deviceWranInfo);
	        pubLogMapper.saveNoramlLogInfo(personName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
	                String.format("修改设备报警编号:{%s} 操作成功!", new Object[] { devWranId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WRAN_MANAGEMENT.getMessage());
	        return devWranId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String updateTimeByWranType(String devCategoryCode, String devTypeCode, String devId, 
    		String wranTypeCode, String wranContent, String personName){
    	try {
    		DeviceWranInfo deviceWranInfo = new DeviceWranInfo(devCategoryCode, null, devTypeCode, 
    				null, devId, null, wranTypeCode, null, null, null, wranContent);
	        deviceWranInfo.setUpdateTime(TimeHelper.dateToLongString());
	    	deviceWranInfoMapper.updateTimeSelective(deviceWranInfo);
	        pubLogMapper.saveNoramlLogInfo(personName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
	                String.format("修改设备报警编号:{%s} 操作成功!", new Object[] { devId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WRAN_MANAGEMENT.getMessage());
	        return devId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String deleteDeviceWranInfo(String devWranId, String personName){
    	try {
    		deviceWranInfoMapper.deleteById(devWranId);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
	                String.format("删除设备报警编号:{%s} 操作成功!", new Object[] { devWranId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WRAN_MANAGEMENT.getMessage());
	        return devWranId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
}
